package com.kdzy.lshy.socity_server.common;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 短信服务
 * @author cly
 * @since 2018-07-04
 */
public class HttpToSMS {

    private static final Logger logger = LoggerFactory.getLogger(HttpToSMS.class);

    /**
     * 短信服务 - 发送短信 - 预约业务状态修改
     * @param param 电话、内容等信息
     * @return 响应结果
     */
    public static Object sendSMS(String param) {
        logger.info("进入《短信服务 - 发送短信 - 预约业务状态修改》sendSMS()方法，入参：" + param);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post =new HttpPost("http://localhost:8086/SMS/sendSMS");

        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("param",param));

        StringEntity entity = null;
        String string = null;
        try {
            entity = new UrlEncodedFormEntity(kvList,"utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response =httpClient.execute(post);
            string = EntityUtils.toString(response.getEntity());

            response.close();
            httpClient.close();

            return string;
        } catch (Exception e) {
            logger.error("《短信服务 - 发送短信 - 预约业务状态修改》sendSMS()方法，发送短信服务异常：" + ExceptionDetailMessage.getExceptionDetail(e));
            return null;
        }
    }

    /**
     * 短信服务 - 发送短信 - 上门审核
     * @param param 电话、内容等信息
     * @return 响应结果
     */
    public static Object sendSMSToHome(String param) {
        logger.info("进入《短信服务 - 发送短信 - 上门审核》sendSMSToHome()方法，入参：" + param);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post =new HttpPost("http://localhost:8086/SMS/sendSMSToHome");

        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("param",param));

        StringEntity entity = null;
        String string = null;
        try {
            entity = new UrlEncodedFormEntity(kvList,"utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response =httpClient.execute(post);
            string = EntityUtils.toString(response.getEntity());

            response.close();
            httpClient.close();

            return string;
        } catch (Exception e) {
            logger.error("《短信服务 - 发送短信 - 上门审核》sendSMSToHome()方法，发送短信服务异常：" + ExceptionDetailMessage.getExceptionDetail(e));
            return null;
        }
    }

    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post =new HttpPost("http://localhost:8080/oauth/token");


        List<NameValuePair> kvList = new ArrayList<NameValuePair>();
        kvList.add(new BasicNameValuePair("clientId:secret","test:123456"));
//        kvList.add(new BasicNameValuePair("secret","123456"));
        kvList.add(new BasicNameValuePair("grant_type","password"));
        kvList.add(new BasicNameValuePair("username","test-user1"));
        kvList.add(new BasicNameValuePair("password","123451"));

        StringEntity entity = null;
        String string = null;
        try {
            entity = new UrlEncodedFormEntity(kvList,"utf-8");
            post.setEntity(entity);
            CloseableHttpResponse response =httpClient.execute(post);
            string = EntityUtils.toString(response.getEntity());
            System.out.println(string);
            response.close();
            httpClient.close();

        } catch (Exception e) {
            logger.error("《短信服务 - 发送短信 - 预约业务状态修改》sendSMS()方法，发送短信服务异常：" + ExceptionDetailMessage.getExceptionDetail(e));
        }
    }
}
